package com.cwt_taxi_xa.db.dal;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cwt_taxi_xa.model.ContactInfo;
import com.cwt_taxi_xa.util.AppLog;

import java.util.ArrayList;

public class ContactDao {
	private ContentValues values;
	private DBOpenHelper helper;
	private static final String TAG = "ContactDao";

	public ContactDao(Context context){
		values = new ContentValues();
		helper = new DBOpenHelper(context);
	}

	public synchronized ArrayList<ContactInfo> getContacts() {
		ArrayList<ContactInfo> contacts = null;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			db.beginTransaction();
			// 执行查询
			Cursor c = db.query(DBConstants.TABLE_CONTACTS,
					null, null, null, null, null, null);
			// 遍历结果
			if (c != null) {
				contacts = new ArrayList<ContactInfo>();
				while (c.moveToNext()) {
					ContactInfo contact = new ContactInfo();
					contact.setId(c.getInt(c.getColumnIndex(DBConstants.CONTACTS_ID)));
					contact.setType(c.getInt(c.getColumnIndex(DBConstants.CONTACTS_TYPE)));
					contact.setName(c.getString(c
							.getColumnIndex(DBConstants.CONTACTS_NAME)));
					contact.setPhone(c.getString(c
							.getColumnIndex(DBConstants.CONTACTS_PHONE)));
					contacts.add(contact);
				}
			}
			//设置事务标志为成功，当结束事务时就会提交事务
			db.setTransactionSuccessful();
			db.endTransaction();
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return contacts;
	}

	public synchronized int save(ArrayList<ContactInfo> contacts) {
		// TODO Auto-generated method stub
		int count = 0;
		long rowId = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			for(int i = 0;i<contacts.size();i++){
				ContactInfo contact = contacts.get(i);
				values.clear();
				values.put(DBConstants.CONTACTS_TYPE, contact.getType());
				values.put(DBConstants.CONTACTS_NAME, contact.getName());
				values.put(DBConstants.CONTACTS_PHONE, contact.getPhone());

				rowId = db.insert(DBConstants.TABLE_CONTACTS, null, values);
				if(rowId != 0){
					AppLog.i(TAG, "添加联系人信息，rowId："+rowId);
				}
			}
			//设置事务标志为成功，当结束事务时就会提交事务
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return count;
		}
		return count;
	}

	public synchronized int update(ArrayList<ContactInfo> contacts) {
		// TODO Auto-generated method stub
		int count = 0;
		long rowId = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			for(int i = 0;i<contacts.size();i++){
				ContactInfo contact = contacts.get(i);
				values.clear();
				values.put(DBConstants.CONTACTS_TYPE, contact.getType());
				values.put(DBConstants.CONTACTS_NAME, contact.getName());
				values.put(DBConstants.CONTACTS_PHONE, contact.getPhone());

				Cursor c = db.query(DBConstants.TABLE_CONTACTS, null, DBConstants.CONTACTS_NAME+"=?", new String[]{contact.getName()}, null, null, null);
				if(c != null && c.moveToFirst()){
					int id = c.getInt(c.getColumnIndex(DBConstants.CONTACTS_ID));
					rowId = db.update(DBConstants.TABLE_CONTACTS, values,
							DBConstants.CONTACTS_ID+"=?", new String[]{""+id});
				}else{
					rowId = db.insert(DBConstants.TABLE_CONTACTS, null, values);
				}
				if(rowId != 0){
					AppLog.i(TAG, "更新联系人信息，rowId："+rowId);
				}
			}
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return count;
		}
		return count;
	}

	public synchronized int clear() {
		// TODO Auto-generated method stub
		int result = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		result = db.delete(DBConstants.TABLE_CONTACTS, null, null);
		//		db.execSQL("delete from sqlite_sequence where name = \"" + Constants.TABLENAME+ "\"");
		db.execSQL("update sqlite_sequence set seq = 0 where name = \""+ DBConstants.TABLE_CONTACTS+ "\"");
		AppLog.i(TAG, "清空表数据: tableName"+DBConstants.TABLE_CONTACTS+"; result:"+result);
		db.close();
		return result;
	}

	public synchronized int refresh(ArrayList<ContactInfo> contacts) {
		// TODO Auto-generated method stub
		clear();
		int count = save(contacts);
		return count;
	}

	public boolean isIncomingContact(String phone) {
		// TODO Auto-generated method stub
		boolean isIncomingContact = false;
		int type = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			Cursor c = db.query(DBConstants.TABLE_CONTACTS, null, DBConstants.CONTACTS_PHONE+"=?",
					new String[]{phone}, null, null, null);
			if(c != null && c.moveToFirst()){
				type = c.getInt(c.getColumnIndex(DBConstants.CONTACTS_TYPE));
				if(type == ContactInfo.TYPE_INCOMING || type == ContactInfo.TYPE_NORMAL){
					isIncomingContact = true;
				}
			}
			db.close();
			AppLog.i(TAG, "type: "+type+"; isIncomingContact："+isIncomingContact);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			AppLog.i(TAG, "判断能否呼入出错！");
		}
		return isIncomingContact;
	}

	public boolean isUnIncomingContact(String phone) {
		// TODO Auto-generated method stub
		boolean unIncomingContact = false;
		int type = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			Cursor c = db.query(DBConstants.TABLE_CONTACTS, null, DBConstants.CONTACTS_PHONE+"=?",
					new String[]{phone}, null, null, null);
			if(c != null && c.moveToFirst()){
				type = c.getInt(c.getColumnIndex(DBConstants.CONTACTS_TYPE));
				if(type == ContactInfo.TYPE_OUT_CALL){
					unIncomingContact = true;
				}
			}
			db.close();
			AppLog.i(TAG, "type: "+type+"; isIncomingContact："+unIncomingContact);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			AppLog.i(TAG, "判断能否呼入出错！");
		}
		return unIncomingContact;
	}
}
